<?php

namespace app\common\model;

use app\admin\model\Admin;
use think\Model;

class UserFinanceLog extends Model
{
    // 表名
    protected $name = 'user_finance_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'flag_text',
        'event_text',
        'status_text'
    ];

    public function getTypeList()
    {
        return ['score' => __('积分'), 'money' => __('余额')];
    }

    public function getFlagList()
    {
        return ['add' => __('增加'), 'reduce' => __('减少')];
    }

    public function getEventList()
    {
        return ['pay' => __('Event pay'), 'groups' => __('Event groups'), 'groups_refund' => __('Event groups_refund'), 'recharge' => __('Event recharge'), 'withdraw' => __('Event withdraw'), 'refund' => __('Event refund'), 'sys' => __('Event sys')];
    }

    public function getStatusList()
    {
        return ['waitting' => __('待审核'), 'succeed' => __('已审核')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['type'] ?? '');
        $list = $this->getTypeList();
        return $list[$value] ?? '';
    }

    public function getFlagTextAttr($value, $data)
    {
        $value = $value ?: ($data['flag'] ?? '');
        $list = $this->getFlagList();
        return $list[$value] ?? '';
    }

    public function getEventTextAttr($value, $data)
    {
        $value = $value ?: ($data['event'] ?? '');
        $list = $this->getEventList();
        return $list[$value] ?? '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public static function addRecord($data)
    {
        $model = new self();
        $model->allowField(true)->saveAll($data);
    }

    public static function updateRecord($id, $data)
    {
        self::update($data, ['id' => $id]);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function admin()
    {
        return $this->belongsTo(Admin::class, 'admin_id');
    }
}
